package com.car.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.nodes.Document;


public class Util {
	
	//给定字符串和正则表达式，判断是否匹配
	public static boolean isMatched(String str,String regex){
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
//		System.out.println(matcher);
		return matcher.matches();
	}
	//给定字符串和正则表达式，获得匹配的字�?
	public static Matcher getMatched(String str,String regex){
		Matcher match = null;
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
//		System.out.println(matcher);
		if(matcher.find()) match=matcher;
		return match;
		
	}
	
	// 根据口碑id得到该口碑页�?
	public static int getAutoHomeKbPage(String iwomId) {
		int page = 0;
		String url = "http://k.autohome.com.cn/" + iwomId + "/index_1.html";
		Document doc = DocUtil.getDoc(url);
		String pages;
		try {
			String p = doc.select("div.page span.page-item-info").text();
			pages = p.substring(1, p.length() - 1);
		} catch (Exception e) {
			pages = "1";
		}
		page = Integer.parseInt(pages);
		return page;
	}
	
	// 根据论坛id得到该帖子的页数
	public int getPages(int uid) {
		int pages = 0;
		String url = "http://i.service.autohome.com.cn/clubapp/OtherTopic-" + uid + "-all-1.html";
		Document doc = DocUtil.getDoc(url);
		if (doc.select("div.paging a:not(.next)").size() != 0) {

			pages = doc.select("div.paging a:not(.next)").size();
		} else {
			pages = 1;
		}
		return pages;
	}
	
	//获得格式化的时间
	public static Date getDate(String date) {
		Date time = null;
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			time = format.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		java.sql.Date sqlDate = new java.sql.Date(time.getTime());
		return sqlDate;
	}

}
